/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2015/5/28.
 */
Rsd.define('Rsd.form.TextArea', {
    extend: 'Rsd.control.Component',
    xtype: 'textarea',
    height: 25,
    width: 80,
    ctrlTagName: 'textarea',
    ctrlCls: 'x-control-textarea',
    dataIndex: '',
    handler: null,
    constructor: function constructor(config) {
        config = config || {};
        this.apply(config);
    },
    /*
     * */
    onAfterInit: function onAfterInit() {
        this.callParent();
        var me = this;
        me.dom.style.textAlign = 'left';
        //position = 'absolute';
        me.ctrl.element.style.resize = "none";
        me.ctrl.element.name = me.dataIndex;
        me.ctrl.element.readonly = me.readonly;
        me.ctrl.element.disabled = me.disabled;
        me.ctrl.element.required = me.required ? 'required' : '';
        me.ctrl.element.placeholder = (me.placeholder || '') + (me.required ? '(必填)' : '');
        me.ctrl.element.rows = me.rows || 1;
        //
        if (me.onchange || me.valueChanged || me.textChanged) {
            me.ctrl.element.onchange = function (evt) {
                me.funApplyByIOC(me.onchange || me.valueChanged || me.textChanged, [me, evt]);
            }
        }
        if (me.onblur) {
            me.ctrl.element.onblur = function (evt) {
                me.funApplyByIOC(me.onblur, [me, evt]);
            }
        }
        if (me.value != undefined) {
            //firefox 不支持 innerText
            me.ctrl.element.innerHTML = me.value;
            me.ctrl.element.value = me.value;
        }

    },
    /*
    * */
    setValue: function (value) {

        if (arguments.length == 0) {
            return;
        }
        this.value = value;
        if (this.ctrl.element) {
            this.ctrl.element.value = value;
        }

    },
    /*
     * */
    getValue: function getValue() {
        return this.ctrl.element.value || this.value;
    },
    /*
     * 返回 对象或数组
     * */
    makeControl: function makeControl(config, row) {
        if (Rsd.isEmpty(row)) {
            return [];
        }

        var _config = config || {};
        var _editable = _config.editable;
        var _dataIndex = _config.dataIndex || _config.dataindex || _config.name;
        var _value = row[_dataIndex];
        
        //debugger; 
        var editor = _config.editor || {};
        var _txt = editor.text || editor.title || _config.text || _config.title || "修改";
        var _ctrl = document.createElement('a');
        _ctrl.innerHTML = "✏️[" + _txt + "]";
        _ctrl.style.float = 'right';
        _ctrl.href = "javascript:void(0)";
        _ctrl.onclick = function () {
            if (Rsd.isEmpty(Rsd.app.downloadService)) {
                alert('未设置app.downloadService');
                return;
            }
            if (Rsd.isEmpty(Rsd.app.uploadService)) {
                alert('未设置app.uploadService');
                return;
            }
            var viewerType = editor.viewerType;//默认上传文件

            var arr = editor.contentType.split('/');
            var media = arr[0];
            var fileType = arr[1];

            if (!viewerType) {
                viewerType = "Rsd.tool.FileSelector";

                if (media == 'application' && fileType == 'json') {
                    viewerType = editor.viewerType || 'Rsd.tool.RichTextEditor';
                }
                if (media == 'text') {
                    //文本文件
                    viewerType = editor.viewerType || 'Rsd.tool.RichTextEditor';
                    if (fileType == 'markdown') {
                        viewerType = editor.viewerType || 'Rsd.tool.MarkdownEditor';
                    }
                }
                if (media == 'image') {
                    //图片文件
                    viewerType = editor.viewerType || 'Rsd.tool.ImageSelector';
                }
            }

           Rsd.app.getSchema(editor.modelType,function(schema){

                if(schema)
                {
                    Rsd.create(viewerType, {
                        title: _txt,
                        contentType: editor.contentType,
                        modelType: editor.modelType,
                        key: row[editor.keyMember || 'Key'],
                        fieldName: editor.dataIndexMember,
                        readonly: _editable === false,
                        width: '80%',
                        height: '80%',
                        downloadUrl: schema.downloadService || '',
                        uploadUrl: schema.uploadService || ''
                    }).showDialog();

                }
                
           }); 
        }
 
        return [_ctrl];
    }
}, function (type) {


    var _editorGetter = function () {

        return this.___editor;
    };

    var _editorSetter = function (value) {

        if (Rsd.isEmpty(value)) {
            this.___editor = null;
            return;
        }
        this.___editor = Rsd.apply({
            //className 如： 'Rsd.tool.RichTextEditor'
            viewerType: 'Rsd.tool.RichTextEditor',
            //对象类型
            modelType: null,
            /**
             * 对象主键
             */
            keyMember: 'Key',
            /**
             * text字段名称
             */
            dataIndexMember: '',
            contentType: 'html',
            style: null,
            /**
             * 编辑器窗体标题
             */
            title: '',
            handler: 'btn_show_editor'

        }, value);
    }

    this.defineProperty(type, "editor", _editorGetter, _editorSetter, false);
});